//
//  SISSChooseView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/10.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSChooseView.h"

@interface SISSChooseView ()

/* leftLabel */
@property (weak, nonatomic) UILabel *leftLabel;
/* rightTextField */
@property (weak, nonatomic) UITextField *rightTextField;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSChooseView

- (void)initUI {
    [super initUI];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = SECONDARY_TEXT_COLOR;
    leftLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:leftLabel];
    self.leftLabel = leftLabel;
    
    UITextField *rightTextField = [[UITextField alloc] init];
    rightTextField.font = [UIFont systemFontOfSize:16];
    rightTextField.userInteractionEnabled = NO;
    rightTextField.textColor = MAIN_TEXT_COLOR;
    rightTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];

    rightTextField.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:rightTextField];
    self.rightTextField = rightTextField;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_list_more"]];
    [self addSubview:imageView];
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    bottomLine.hidden = !self.isShowBottomLine;
    [self addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    // 自动布局
    CGFloat margin = 8.0;
    WeakSelf
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(1.5 * margin);
        make.left.equalTo(weakSelf).offset(2 * margin);
    }];
    
    [self.rightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf).offset(20 * margin);
//        make.right.equalTo(imageView.mas_left);
        make.centerX.equalTo(weakSelf).offset(6 * margin);
        make.centerY.equalTo(weakSelf);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf);
        make.centerY.equalTo(weakSelf);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf);
        make.top.equalTo(weakSelf.leftLabel.mas_bottom).offset(1.5 * margin);
        make.height.mas_equalTo(1);
    }];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickAction)];
    [self addGestureRecognizer:tapGesture];
}

- (void)tapClickAction {
    
    if ([self.delegate respondsToSelector:@selector(clickChooseActionWithChooseView:)]) {
        [self.delegate clickChooseActionWithChooseView:self];
    }
}

- (void)setIsShowBottomLine:(BOOL)isShowBottomLine {
    _isShowBottomLine = isShowBottomLine;
    self.bottomLine.hidden = !isShowBottomLine;
}

- (void)setLeftStr:(NSString *)leftStr {
    _leftStr = leftStr;
    if ([[leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.leftLabel.attributedText = attributedStr;
    } else {
        self.leftLabel.text = leftStr;
    }
    
}

- (void)setRightPlaceStr:(NSString *)rightPlaceStr {
    _rightPlaceStr = rightPlaceStr;
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:rightPlaceStr attributes:
                                      @{NSForegroundColorAttributeName : SECONDARY_TEXT_COLOR
                                        }];
    self.rightTextField.attributedPlaceholder = attrString;
//    self.rightTextField.placeholder = rightPlaceStr;
}

- (void)setRightStr:(NSString *)rightStr {
    _rightStr = rightStr;
    self.rightTextField.text = rightStr;
}

- (void)setLeftStrColor:(UIColor *)leftStrColor {
    _leftStrColor = leftStrColor;
    self.leftLabel.textColor = leftStrColor;
}

@end
